package cc.mrbird.messageNotice.controller;

import cc.mrbird.common.annotation.Log;
import cc.mrbird.common.controller.BaseController;
import cc.mrbird.common.domain.QueryRequest;
import cc.mrbird.common.domain.ResponseBo;
import cc.mrbird.messageNotice.domain.MessageHistory;
import cc.mrbird.messageNotice.domain.MessageNoticeEntry;
import cc.mrbird.messageNotice.service.MessageNoticeService;
import cc.mrbird.submission.util.RestUtil;
import cc.mrbird.submission.util.WXPayUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class MessageNoticeController extends BaseController {

    @Autowired
    private MessageNoticeService messageNoticeService;

    @RequestMapping("messageNotice")
    public String messageNotice(Model model, MessageNoticeEntry messageNoticeEntry) {

        return "messageNotice/messageNotice";
    }

    @Log("增加短信模板")
    @RequestMapping("messageNotice/addMessageNoticeEntry")
    @RequiresPermissions("submission:addPullDown")
    @ResponseBody
    public ResponseBo addMessageNoticeEntry(MessageNoticeEntry messageNoticeEntry ){
        System.out.println(messageNoticeEntry);
        messageNoticeService.addMessageNoticeEntry(messageNoticeEntry);
        return ResponseBo.ok();
    }

    @Log("获取标题下拉选")
    @RequestMapping("messageNotice/sendMessageNoticeTitle")
    @RequiresPermissions("submission:addPullDown")
    @ResponseBody
    public List<MessageNoticeEntry> sendMessageNoticeTitle(){

        return messageNoticeService.sendMessageNoticeTitle();
    }

    /**
     * 投诉系统发送短信给客户
     * @return
     * @throws Exception
     */
    @RequestMapping("messageNotice/sendMessageNotice")
    @ResponseBody
    public ResponseBo sendMessageNotice(MessageNoticeEntry messageNoticeEntry) throws Exception {
        MessageNoticeEntry messageNotice = messageNoticeService.getByIdMessageNoticeEntry(messageNoticeEntry.getMessageNoticeTitle());
        String phone = messageNoticeEntry.getPhone();
        String messageTemplate = messageNotice.getMessageTemplate();
        System.out.println(messageTemplate);
        WXPayUtil wxPayUtil = new WXPayUtil();
        Map map = new HashMap();
        String idNotice = "110";
        String keyid = "2";
        String random_number = wxPayUtil.generateNonceStr();
        String time_stamp = String.valueOf(System.currentTimeMillis());
        String key = "EL6UjSc4U1it6vaFrQ9GlGiMicEbRc0P";
        //通知编号
        map.put("id",idNotice);
        //keyid
        map.put("keyid",keyid);
        //电话号码
        map.put("phone",phone);
        //随机数
        map.put("random_number",random_number);
        //时间戳
        map.put("time_stamp",time_stamp);
        String sign = wxPayUtil.generateSignature(map,key);
        String url = "http://120.77.182.149:8999/OpenApi/sendMessageNotice";
        //String url = "http://localhost:8999/OpenApi/sendMessageNotice";
        System.out.println(url);
        String data = "id="+idNotice+"&phone="+phone+"&keyid="+keyid+"&random_number="+random_number+"&time_stamp="+time_stamp+"&sign="+sign+"&messageTemplate="+messageTemplate;
        System.out.println("data:"+data);
        RestUtil restUtil = new RestUtil();
        restUtil.interfaceUtil(url,data);
        MessageHistory messageHistory = new MessageHistory();
        messageHistory.setMessageNoticeTitle(messageNotice.getMessageNoticeTitle());
        messageHistory.setMessageNoticeValue(messageNotice.getMessageNoticeValue());
        messageHistory.setPhone(phone);
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(currentTime);
        messageHistory.setMessageNoticeTime(dateString);
        messageHistory.setName(messageNoticeEntry.getName());
        messageNoticeService.addMessageHistory(messageHistory);
        return ResponseBo.ok();
    }

    @Log("获取内容下拉选")
    @RequestMapping("messageNotice/sendMessageValue")
    @RequiresPermissions("submission:addPullDown")
    @ResponseBody
    public MessageNoticeEntry sendMessageValue(MessageNoticeEntry messageNoticeEntry){
        return messageNoticeService.sendMessageValue(messageNoticeEntry.getId());

    }

    @Log("短信通知列表")
    @RequestMapping("messageNotice/messageHistoryList")
    @ResponseBody
    public Map<String, Object> messageHistoryList(QueryRequest request) {

        return super.selectByPageNumSize(request, () -> this.messageNoticeService.messageHistoryList());
    }

}
